import datasetService from '@/api/services/datasetService';
import FilePreviewer from '@/pages/components/fileviewer/FilePreviewer';
import ImagePreviewer from '@/pages/components/fileviewer/ImagePreviewer';
import VideoFilePreviewer from '@/pages/components/fileviewer/VideoFilePreviewer';
import { memo, useCallback, useEffect, useState } from 'react';
import { useContextSelector } from 'use-context-selector';
import { CollectionContext } from '../../context';
import Markdown from '@/components/markdown';
import { CircleLoading } from '@/components/loading';
import { message } from 'antd';
import AppManagerIcon from '@/assets/icons/ic_app_manager.svg';

const PreviewCollection = () => {
  const [url, setPreviewUrl] = useState('');
  const [fileName, setFileName] = useState('');
  const [isError, setIsError] = useState(false);
  const [loading, setLoading] = useState(false);
  const { collectionId } = useContextSelector(CollectionContext, (s) => s);
  const [messageApi, contextHolder] = message.useMessage();
  const getPreviewUrl = useCallback(async () => {
    if (!collectionId) {
      return;
    }
    try {
      const res = await datasetService.collectionPreviewInfo({ collection_id: collectionId });
      if (res.type == 'link') {
        window.open(res.url);
      } else if (res.type == 'file') {
        setLoading(true);
        setFileName(res.filename);
        // setIsPreview(true);
        if (res.filename.endsWith('.md')) {
          setPreviewUrl(res.file_url);
        } else {
          setPreviewUrl(`${res.url}`);
        }
      }
    } catch (error) {
      console.log(error);
      // messageApi.error(error.message);  去掉这个，已经有了
    }
  }, [collectionId]);

  useEffect(() => {
    setIsError(false);
    getPreviewUrl();
  }, [collectionId]);

  return (
    <div className="h-full">
      {contextHolder}
      {url && (
        <FilePreviewer
          previewUrl={url}
          fileName={fileName}
          loading={loading}
          setLoading={setLoading}
        />
      )}
      {/* {url && (
        <ImagePreviewer
          imageUrl={AppManagerIcon}
          minScale={0.1} // 最小缩放0.8倍
          maxScale={10}   // 最大缩放2倍
        />
      )} */}
      {/* {url && (
        <VideoFilePreviewer
          previewUrl={url}
          fileName={fileName}
          loading={loading}
          setLoading={setLoading}
        />
      )} */}



      {loading && (
        <div className="flex h-full items-center justify-center overflow-y-auto rounded-lg bg-[#FFF] p-6">
          <CircleLoading />
        </div>
      )}
    </div>
  );
};

export default memo(PreviewCollection);
